package com.aishua.fire.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.aishua.fire.jpa.PermteamDao;
import com.aishua.fire.common.constant.SoftdelConstants;
import com.aishua.fire.jpa.PermDao;
import com.aishua.fire.jpa.PermPermteamDao;
import com.aishua.fire.jpa.entity.Perm;
import com.aishua.fire.jpa.entity.PermPermteam;
import com.aishua.fire.jpa.entity.PermPermteamId;
import com.aishua.fire.jpa.entity.Permteam;
import com.aishua.fire.mapper.PermteamMapper;
import com.aishua.fire.mapper.dto.PermPermteamDto;
import com.aishua.fire.mapper.dto.PermteamDto;
import com.aishua.fire.service.PermteamService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class PermteamServiceImpl implements PermteamService{
	
	@Autowired
	private PermteamDao permteamdao;
	@Autowired
	private PermPermteamDao permteampermdao;
	@Autowired
	private PermDao permdao;
	@Autowired
	private PermteamMapper permteamMapper;
	/**
	 * 添加权限组信息
	 * @param permissiondto
	 * 
	 */
	/*@Override
	public void addPermteamInfo(PermteamInfo permteaminfo) {
		
		Permteam permteam = new Permteam(permteaminfo.getPermteamName(),permteaminfo.getPermteamDesc(),new Date(),SoftdelConstants.DEL_N);
		
		permteamdao.save(permteam);
		
	}*/
	@Override
	public void addPermteamInfo(PermteamDto permteamDto) {
		
		permteamMapper.addPermteamInfo(permteamDto);
	}

	/**
	 * 更新权限组信息
	 * @param permteaminfo
	 * 
	 */
	/*@Override
	public void updatePermteamInfo(PermteamInfo permteaminfo) {
		
		Permteam permteam = new Permteam();
		
		Long permteamid = Long.parseLong(permteaminfo.getPermteamId());
		
		permteam.setPermteamId(permteamid);
		permteam.setPermTeamName(permteaminfo.getPermteamName());
		permteam.setPermTeamDesc(permteaminfo.getPermteamDesc());
		permteam.setCreateDate(new Date());
		permteam.setStatus(SoftdelConstants.DEL_N);
		permteamdao.saveAndFlush(permteam);
	}*/
	public void updatePermteamInfo(PermteamDto permteamDto) {
		
		permteamMapper.updatePermteamInfo(permteamDto);
	}
	/**
	 * 删除对应限组信息
	 * @param permTeamId
	 * 
	 */
	/*@Override
   public void deletePermteamInfo(String permTeamId) {
		
	    
		
		Long permteamid = Long.parseLong(permTeamId);
		
		Permteam permteam = permteamdao.findOne(permteamid);
		
		permteam.setPermteamId(permteamid);
		permteam.setStatus(SoftdelConstants.DEL_Y);
		permteamdao.saveAndFlush(permteam);
		
		
	}*/
	@Override
	public void deletePermteamInfo(PermteamDto permteamDto) {
		
		permteamMapper.deletePermteamInfo(permteamDto);
	}
	
	/**
	 * 分页列表查询
	 */
	/*@Override
	public Page<Permteam> searchList(Integer page, Integer size,final String permTeamName){
		Pageable pageable = (Pageable) new PageRequest(page, size, new Sort("permTeamName"));
		return permteamdao.findAll(new Specification<Permteam>() {
            
			@Override
            public Predicate toPredicate(Root<Permteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
               
                Path<String> userNamePath = root.get("permTeamName");
                *//**
                 * 连接查询条件, 不定参数，可以连接0..N个查询条件
                 *//*
                
                query.where(cb.like(userNamePath, "%" + permTeamName + "%")); //这里可以设置任意条查询条件
                return null;
            }
        },pageable);
	}*/
	
	public  List<Permteam> searchList(){
		
		return permteamdao.searchList();
	}
	
	/**
	 * 权限组通过权限id查询权限组信息
	 */
	@Override
	public  List<Permteam> getPermteamListByPermId(final String permId){
		
     final List<PermPermteam> PermPermteamList = permteampermdao.findAll(new Specification<PermPermteam>() {
            
			@Override
            public Predicate toPredicate(Root<PermPermteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<PermPermteamId> userNamePath = root.get("permpermteamId");
                //连接查询条件, 不定参数，可以连接0..N个查询条件
                query.where(cb.equal(userNamePath.get("permId").as(Long.class), permId)); //这里可以设置任意条查询条件
                return null;
            }
        });
		
		final List<Long> longs = new ArrayList<>();
		for(int i = 0;i<PermPermteamList.size();i++) {
			longs.add(PermPermteamList.get(i).getPermpermteamId().getPermteamId());
		}
		
		log.info("PermPermteamList的值是:{}",PermPermteamList);
		return permteamdao.findAll(new Specification<Permteam>() {
		    
			@Override
		    public Predicate toPredicate(Root<Permteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				Path<String> statusPath = root.get("status");
				
				query.where(root.get("permteamId").in(longs),cb.equal(statusPath,SoftdelConstants.DEL_N));
				
		        return null;
		    }
		});

		
	}
	
	/**
	 * 权限组通过权限组id查询权限信息
	 */
	/*public List<Perm>  getPermListByPermteamId(){
		
		
		
		
	     return	 permteamdao.getPermListByPermteamId();
	}*/
	
	/*@Override
	public  List<Perm> getPermListByPermteamId(final String permteamId){
		
     final List<PermPermteam> PermPermteamList = permteampermdao.findAll(new Specification<PermPermteam>() {
            
			@Override
            public Predicate toPredicate(Root<PermPermteam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<PermPermteamId> userNamePath = root.get("permpermteamId");
                //连接查询条件, 不定参数，可以连接0..N个查询条件
                query.where(cb.equal(userNamePath.get("permteamId").as(Long.class), permteamId)); //这里可以设置任意条查询条件
                return null;
            }
        });
		
		final List<Long> longs = new ArrayList<>();
		for(int i = 0;i<PermPermteamList.size();i++) {
			longs.add(PermPermteamList.get(i).getPermpermteamId().getPermId());
		}
		
		log.info("PermPermteamList的值是:{}",PermPermteamList);
		return permdao.findAll(new Specification<Perm>() {
		    
			@Override
		    public Predicate toPredicate(Root<Perm> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				Path<String> statusPath = root.get("status");
				
				query.where(root.get("permId").in(longs),cb.equal(statusPath,SoftdelConstants.DEL_N));
				
		        return null;
		    }
			
		});

		
	}*/
	
	@Override
	public  List<PermPermteamDto> getPermListByPermteamId(String permteamId){
		
		return permteamMapper.getPermListByPermteamId(Long.parseLong(permteamId));
	}
	
	@Override
	public List<Perm> getPermallList(String permteamId){
		List<Perm> perms = permdao.getPermallList(permteamId);
		for (int i = 0; i < perms.size(); i++) {
			perms.get(i).setPermDesc(perms.get(i).getTotalModuleName()+"-"+perms.get(i).getModuleName()+"-"+perms.get(i).getPermDesc());
		}
		return perms;
	}

	//查询id,name集合
	@Override
	public List<Permteam> findIdAndPermTeamName() {
		List<Permteam> permteams = permteamMapper.findIdAndPermteamName();
		return permteams;
	}
	
}
